package com.jml.SessionAttribute;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/pathVariable")
public class PathVariableController {

    @RequestMapping(value = "/student/{studentID}",method = RequestMethod.GET)
    public void getStudent(@PathVariable("studentID") Long studentID){
        System.out.println(studentID);
    }

    @RequestMapping(value = "/student/{studentID}/book/{bookId}",method = RequestMethod.GET)
    public void getStudentBook(@PathVariable("studentID") Long studentID,@PathVariable("bookId")String bookId){
        System.out.println(studentID);
        System.out.println(bookId);
    }

    @RequestMapping(value = "/book/{id}",method = RequestMethod.GET)
    public void getBook(@PathVariable("id")String bookId){
        System.out.println(bookId);
    }

    // uri是：/matrixVariable/student/zhangsan;sex=男
    @RequestMapping(value = "/student/{studentName}",method = RequestMethod.GET)
    public void getStudentAndSex(@PathVariable("studentName") String studentName,@MatrixVariable String sex){
        System.out.println(studentName);
        System.out.println(sex);
    }

    // uri是：/matrixVariable/student/zhangsan;sex=男;group=gg
    @RequestMapping(value = "/matrixVariable/student/{studentName}",method = RequestMethod.GET)
    public void getStudentAndSexAndGroup(@PathVariable("studentName") String studentName,
                                 @MatrixVariable String sex,
                                 @MatrixVariable(required=false) String group){
        System.out.println(studentName);
        System.out.println(sex);
        System.out.println(group);
    }

    //MatrixVariable接多个属性用set接受
    // uri是：/matrixVariable/student/zhangsan;colors=black,red/tel/1881307;group=gg
    @RequestMapping(value = "/matrixVariable1/student/{studentName}/tel/{telName}",method = RequestMethod.GET)
    public void getStudentAndSexAndGroupSet(@PathVariable("studentName") String studentName,
                                         @PathVariable("telName") String telName,
                                         @MatrixVariable(required = false) Set<String> colors,
                                         @MatrixVariable(required=false) String gender,
                                         @MatrixVariable(required=false) String group){
        System.out.println(studentName);//zhangsan
        System.out.println(telName);    //1881307
        System.out.println(colors);     //black,red
        System.out.println(gender);     //null
        System.out.println(group);      //gg
    }

    //url中不同的值假如有一样的属性，可以使用pathVar指定是哪个值再使用value指定是这个值的哪个属性，
    //@MatrixVariable(required=false,pathVar = "studentName",value = "group") String namGroup --表示是studentName中的group属性
    //@MatrixVariable(required=false,pathVar = "telName",value = "group") String group --表示是telName中的group属性
    //如果只有一个属性就按照定义的变量名字和url中属性的名字一样进行赋值。
    // uri是：/matrixVariable/student/zhangsan;colors=black,red;group=note/tel/1881307;group=gg
    @RequestMapping(value = "/matrixVariable2/student/{studentName}/tel/{telName}",method = RequestMethod.GET)
    public void getStudentAndSexAndGroupPathVar(@PathVariable("studentName") String studentName,
                                            @PathVariable("telName") String telName,
                                            @MatrixVariable(required = false) Set<String> colors,
                                            @MatrixVariable(required=false) String gender,
                                            @MatrixVariable(required=false,pathVar = "studentName",value = "group") String namGroup,
                                            @MatrixVariable(required=false,pathVar = "telName",value = "group") String group){
        System.out.println(studentName);//zhangsan
        System.out.println(telName);    //1881307
        System.out.println(colors);     //black,red
        System.out.println(gender);     //null
        System.out.println(group);      //gg
    }

    //matrixVariable存放Map
    // uri是：/matrixVariable/student/zhangsan;colors=black,red;group=note/tel/1881307;group=gg;sex=mm
    @RequestMapping(value = "/matrixVariable3/student/{studentName}/tel/{telName}",method = RequestMethod.GET)
    public void getStudentAndSexAndGroupMap(@PathVariable("studentName") String studentName,
                                            @PathVariable("telName") String telName,
                                            @MatrixVariable Map<String ,String> matrixVars,
                                            @MatrixVariable(pathVar = "studentName") Map<String ,String> matrixVarsStudentName,
                                            @MatrixVariable(pathVar = "telName") Map<String ,String> matrixVarsTelName){
        System.out.println(studentName);            //zhangsan
        System.out.println(telName);                //1881307
        System.out.println(matrixVars);             // { colors:[black,red] , group:[note,gg] , sex:mm}
        System.out.println(matrixVarsStudentName);  // { colors:[black,red] , group:note }
        System.out.println(matrixVarsTelName);      // { group:gg , sex:mm}
    }
}
